module.exports = {
    configureWebpack: {
        devServer: {
            //MOCK接口编写的地方
            //每次做更改这个配置文件的时候，都必须重启项目才会生效
            before(app) {
                //模拟用户列表接口信息+分页
                var userlist = [{
                    "id": 15,
                    "UserName": "李四",
                    "Password": "qwerty",
                    "CreateTime": "2023/5/12 星期五 14:30:00",
                    "RoleName": "运营主管",
                    "Mobile": "13912345678",
                    "Email": "lisi@example.com",
                    "MgState": true
                }, {
                    "id": 14,
                    "UserName": "王五",
                    "Password": "asdfgh",
                    "CreateTime": "2023/4/20 星期四 09:15:00",
                    "RoleName": "客服经理",
                    "Mobile": "13887654321",
                    "Email": "wangwu@company.cn",
                    "MgState": false
                }, {
                    "id": 13,
                    "UserName": "赵六",
                    "Password": "zxcvbn",
                    "CreateTime": "2023/3/8 星期三 16:45:00",
                    "RoleName": "财务专员",
                    "Mobile": "13711223344",
                    "Email": "zhaoliu@finance.com",
                    "MgState": true
                }, {
                    "id": 12,
                    "UserName": "钱七",
                    "Password": "123abc",
                    "CreateTime": "2023/2/14 星期二 11:20:00",
                    "RoleName": "技术总监",
                    "Mobile": "13644556677",
                    "Email": "qianqi@tech.org",
                    "MgState": false
                }, {
                    "id": 11,
                    "UserName": "孙八",
                    "Password": "abc456",
                    "CreateTime": "2023/1/30 星期一 13:10:00",
                    "RoleName": "产品经理",
                    "Mobile": "13577889900",
                    "Email": "sunba@product.com",
                    "MgState": false
                }, {
                    "id": 10,
                    "UserName": "周九",
                    "Password": "789xyz",
                    "CreateTime": "2022/12/25 星期日 10:05:00",
                    "RoleName": "UI设计师",
                    "Mobile": "13466778899",
                    "Email": "zhoujiu@design.cn",
                    "MgState": true
                }, {
                    "id": 9,
                    "UserName": "吴十",
                    "Password": "xyz123",
                    "CreateTime": "2022/11/11 星期五 15:35:00",
                    "RoleName": "测试工程师",
                    "Mobile": "13355667788",
                    "Email": "wushi@qa.com",
                    "MgState": false
                }, {
                    "id": 8,
                    "UserName": "郑十一",
                    "Password": "pass11",
                    "CreateTime": "2022/10/1 星期六 08:00:00",
                    "RoleName": "运维主管",
                    "Mobile": "13244556677",
                    "Email": "zheng11@ops.net",
                    "MgState": true
                }, {
                    "id": 7,
                    "UserName": "王十二",
                    "Password": "word12",
                    "CreateTime": "2022/9/9 星期五 17:55:00",
                    "RoleName": "市场专员",
                    "Mobile": "13133445566",
                    "Email": "wang12@market.com",
                    "MgState": false
                }, {
                    "id": 6,
                    "UserName": "陈十三",
                    "Password": "key13",
                    "CreateTime": "2022/8/8 星期一 12:25:00",
                    "RoleName": "销售经理",
                    "Mobile": "13022334455",
                    "Email": "chen13@sales.cn",
                    "MgState": true
                }]
                //删除用户的接口
                app.get('/api/deleteuser', (req, res) => {
                    const { id, query, pagenum, pagesize } = req.query
                    if (userlist) {
                        for (var i = 0; i < userlist.length; i++) {
                            if (userlist[i].id == id)
                                userlist.splice(i, 1)
                        }
                    }
                    userlist.sort(function (a, b) {
                        return b.id - a.id //把userlist降序排列
                    })

                    var total = userlist.length
                    if (pagenum == 0) {
                        newDataList = userlist.slice(pagenum * pagesize, (parseInt(pagenum) + 1) * pagesize)
                    } else
                        newDataList = userlist.slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)

                    res.json({
                        'status': 'ok',
                        'total': total,
                        'data': newDataList
                    })
                })
                //修改用户信息的接口
                app.get('/api/updateuser', (req, res) => {
                    const { id, username, password, email, mobile, query, pagenum, pagesize } = req.query
                    if (userlist) {
                        userlist.filter(u => u.id == id).forEach(element => {
                            element.UserName = username
                            element.Email = email
                            element.Mobile = mobile
                        })
                    }
                    var total = userlist.length
                    if (pagenum == 0) {
                        newDataList = userlist.slice(pagenum * pagesize, (parseInt(pagenum) + 1) * pagesize)
                    } else
                        newDataList = userlist.slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)

                    res.json({
                        'status': 'ok',
                        'total': total,
                        'data': newDataList
                    })
                })
                //添加用户信息的接口
                app.get('/api/adduser', (req, res) => {
                    const { username, password, email, mobile, query, pagenum, pagesize } = req.query
                    var id = userlist[0].id
                    var adduser = {
                        "id": id + 1,
                        "UserName": username,
                        "Password": password,
                        "CreateTime": "0001/1/1 星期一 0:00:00",
                        "RoleName": "超级管理员",
                        "Mobile": mobile,
                        "Email": email,
                        "MgState": true
                    }
                    userlist.push(adduser)
                    //排序userlist
                    userlist.sort(function (a, b) {
                        return b.id - a.id //把userlist降序排列
                    })
                    total = userlist.length
                    var newDataList = []
                    if (pagenum == 0) {
                        newDataList = userlist.slice(pagenum * pagesize, (parseInt(pagenum) + 1) * pagesize)
                    } else
                        newDataList = userlist.slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)
                    //向前端反馈的数据
                    res.json({
                        'total': total,
                        'data': newDataList
                    })
                })
                //修改用户状态的接口
                app.get('/api/userupdate', (req, res) => {
                    const { id, MgState } = req.query
                    var count = 0
                    var newState = ''
                    if (userlist) {
                        var user = userlist.filter(u => u.id == id)
                        count = user.length
                        user.MgState = MgState
                        newState = user.MgState
                    }
                    res.json({
                        'count': count,
                        'newState': newState
                    })
                })
                // 查询用户信息接口
                app.get('/api/users', (req, res) => {
                    const { query, pagenum, pagesize } = req.query

                    var total = 0
                    //注意：(parseInt(pagenum) + 1) 两个数相加时，
                    //一定要通过parseInt(pagenum)把传过来的参数转换成int类型
                    //不然如pagenum为1 ，会出现这样错误：pagenum + 1=11。
                    var newDataList = []
                    if (query != '') {
                        total = userlist.filter(u => u.UserName.includes(query)).length
                        if (pagenum == 0) {
                            newDataList = userlist.filter(u => u.UserName.includes(query)).slice(pagenum * pagesize, (parseInt(pagenum) + 1) * pagesize)
                        } else
                            newDataList = userlist.filter(u => u.UserName.includes(query)).slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)
                    } else {
                        total = userlist.length
                        if (pagenum == 0) {
                            newDataList = userlist.slice(pagenum * pagesize, (parseInt(pagenum) + 1) * pagesize)
                        } else
                            newDataList = userlist.slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)
                    }
                    res.json({
                        'status': 'ok',
                        'total': total,
                        'data': newDataList
                    })
                })


                //模拟左侧菜单接口
                app.get('/api/menu', (req, res) => {
                    res.json({
                        "status": "ok",
                        "data": [{
                            "id": 1,
                            "name": "用户管理",
                            "child": [{
                                "id": 1,
                                "name": "用户列表",
                                "path": "/users"
                            }]
                        }, {
                            "id": 2,
                            "name": "权限管理",
                            "child": [{
                                "id": 2,
                                "name": "角色列表",
                                "path": "/rote"
                            }, {
                                "id": 3,
                                "name": "权限列表",
                                "path": "/right"
                            }]
                        }, {
                            "id": 3,
                            "name": "商品管理",
                            "child": [{
                                "id": 4,
                                "name": "商品列表",
                                "path": "/product"
                            }, {
                                "id": 5,
                                "name": "分类参数",
                                "path": "/userss"
                            }, {
                                "id": 6,
                                "name": "商品分类",
                                "path": "/producttype"
                            }]
                        }, {
                            "id": 4,
                            "name": "订单管理",
                            "child": []
                        }, {
                            "id": 5,
                            "name": "数据统计",
                            "child": []
                        }]
                    })
                })

                //模拟登陆的一个接口
                let tockenStr = 'dhclass'
                //(req,res)回调方法，req：请求；res:返回的结果
                app.get('/api/login', (req, res) => {
                    const { username, password } = req.query
                    if (username == 'admin' && password == '123456' ||
                        username == 'dh' && password == '123456') {
                        res.json({
                            code: 0,
                            message: '登陆成功',
                            tocken: tockenStr + '-' +
                                username + '-' +
                                (new Date().getTime() + 60 * 60 * 1000)
                        })
                    } else {
                        res.json({
                            code: 1,
                            message: '账号或密码错误'
                        })
                    }
                })
            }
        }
    }
}